package com.jzt.qf.service.GetService.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jzt.qf.beans.HttpResult;
import com.jzt.qf.beans.UploadBase;
import com.jzt.qf.beans.jztBean.ZxyJsjlJzt;
import com.jzt.qf.beans.jztBean.ZxyJztJsRecords;
import com.jzt.qf.beans.jztBean.ZxyStockStatusChangeJzt;
import com.jzt.qf.beans.jztBean.ZxyYhjlJzt;
import com.jzt.qf.dao.ZxyJsjlJztDao;
import com.jzt.qf.dao.ZxyJztJsRecordsDao;
import com.jzt.qf.dao.ZxyStockStatusChangeJztDao;
import com.jzt.qf.dao.ZxyYhjlJztDao;
import com.jzt.qf.https.JztApi;
import com.jzt.qf.service.GetService.GetJLService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class GetJLServiceImpl implements GetJLService {
    @Resource
    private JztApi jztApi;

    @Override
    public HttpResult getYSJL(UploadBase uploadBase) {
        log.info("获取九州通运输记录" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通运输记录回传" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Override
    public HttpResult getCKFH(UploadBase uploadBase) {
        log.info("获取九州通出库复核报文" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通出库复核回传" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Override
    public HttpResult getRKYS(UploadBase uploadBase) {
        log.info("获取九州通入库验收报文" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通入库验收回传" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Resource
    private ZxyStockStatusChangeJztDao zxyStockStatusChangeJztDao;

    @Override
    public HttpResult getStockType(UploadBase uploadBase) {
        log.info("获取九州通库存状态变化" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通库存状态变化回传" + JSON.toJSONString(httpResult));
        List<ZxyStockStatusChangeJzt> zxyStockStatusChangeJzts =
                JSONArray.parseArray(httpResult.getMsgInfo().toString(), ZxyStockStatusChangeJzt.class);
        for (ZxyStockStatusChangeJzt zxyStockStatusChangeJzt : zxyStockStatusChangeJzts) {
            String billDtlIdJzyc = zxyStockStatusChangeJzt.getBill_Dtl_Id_Jzyc();
            //检索当前的库存变化是否存在,如果存在,不进行插入数据,
            int count = zxyStockStatusChangeJztDao.getZxyStockStatusChangeJztListByBillIdJzt(billDtlIdJzyc);
            if (count == 0) {
                zxyStockStatusChangeJztDao.insertSelective(zxyStockStatusChangeJzt);
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult getLoss(UploadBase uploadBase) {
        log.info("获取九州通损益单报文" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通损益单回传" + JSON.toJSONString(httpResult));
        return httpResult;
    }

    @Resource
    private ZxyJsjlJztDao zxyJsjlJztDao;

    @Override
    public HttpResult getJSRecords(UploadBase uploadBase) {
        log.info("根据单号获取九州通拒收记录" + JSON.toJSONString(uploadBase));
        HttpResult<List<ZxyJsjlJzt>> httpResult = jztApi.HttpRequest(uploadBase);
        log.info("根据单号获取九州通拒收记录" + JSON.toJSONString(httpResult));
        List<ZxyJsjlJzt> jztJsRecordsList = JSONArray.parseArray(JSON.toJSONString(httpResult.getMsgInfo()), ZxyJsjlJzt.class);
        for (ZxyJsjlJzt zxyJztJsRecords : jztJsRecordsList) {
            ZxyJsjlJzt zxyJsjlJztBy = zxyJsjlJztDao.getZxyJsjlJztBy(zxyJztJsRecords.getBill_Dtl_Id_Jzyc());
            if (BeanUtil.isNotEmpty(zxyJsjlJztBy)) {
                log.error("当前拒收记录已经存在" + zxyJztJsRecords.getBill_Dtl_Id_Jzyc());
            } else {
                zxyJztJsRecords.setFlag(0);
                zxyJsjlJztDao.insertSelective(zxyJztJsRecords);
            }
        }
        return httpResult;
    }

    @Resource
    private ZxyYhjlJztDao zxyYhjlJztDao;

    @Override
    public HttpResult getYHJL(UploadBase uploadBase) {
        log.info("获取九州通养护记录报文" + JSON.toJSONString(uploadBase));
        HttpResult httpResult = jztApi.HttpRequest(uploadBase);
        log.info("获取九州通养护记录回传" + JSON.toJSONString(httpResult));
        List<ZxyYhjlJzt> list = JSONArray.parseArray(JSON.toJSONString(httpResult.getMsgInfo()), ZxyYhjlJzt.class);
        for (ZxyYhjlJzt zxyYhjlJzt : list) {
            ZxyYhjlJzt zxyYhjlJztByBillDtlIdJzyc = zxyYhjlJztDao.getZxyYhjlJztByBill_Dtl_Id_Jzyc(zxyYhjlJzt.getBill_Dtl_Id_Jzyc(), zxyYhjlJzt.getBusinessbill_No());
            if (BeanUtil.isNotEmpty(zxyYhjlJztByBillDtlIdJzyc)) {
                log.error("养护记录已经存在,九州通id:" + zxyYhjlJztByBillDtlIdJzyc.getBill_Dtl_Id_Jzyc());
            } else {
                zxyYhjlJzt.setJztStock("河南");
                zxyYhjlJztDao.insertSelective(zxyYhjlJzt);
            }
        }
        return httpResult;
    }
}
